// J2ME Compass
// Copyright (C) 2006 Dana Peters
// http://www.qcontinuum.org/compass

package org.qcontinuum.astro;

import henson.midp.Float;

public class EquitorialPosition {
    
    private Float mRightAscension;
    private Float mDeclination;

    public EquitorialPosition(Float ra, Float dec) {
        mRightAscension = ra;
        mDeclination = dec;
    }
    
    public Float getRightAscension() {
        return mRightAscension;
    }
    
    public Float getDeclination() {
        return mDeclination;
    }

    public HorizontalPosition toHorizontalPosition(EarthPosition earth, Float mjd) {
        Float gmst = UtcDate.GMST(mjd);
        Float lat = Float.toRadians(earth.getLatitude());
        Float lon = Float.toRadians(earth.getLongitude());
	// tau = gmst + lon - RA;
        Float tau = gmst.Add(lon).Sub(mRightAscension);
        // x = cos(tau) * cos(Dec);
        Float x = Float.cos(tau).Mul(Float.cos(mDeclination));
        // y = sin(tau) * cos(Dec);
        Float y = Float.sin(tau).Mul(Float.cos(mDeclination));
        // z = sin(Dec);
        Float z = Float.sin(mDeclination);
        // rotate so z is the local zenith
        // xhor = x * sin(lat) - z * cos(lat);
        Float xhor = x.Mul(Float.sin(lat)).Sub(z.Mul(Float.cos(lat)));
        Float yhor = y;
        // zhor = x * cos(lat) + z * sin(lat);
        Float zhor = x.Mul(Float.cos(lat)).Add(z.Mul(Float.sin(lat)));
        HorizontalPosition skyPosition = new HorizontalPosition(0, 0);
        Float Az, h;
        // Az = atan2(yhor, xhor);
        Az = Float.atan2(yhor, xhor);
        Az = (Az.Great(Float.PI)) ? Az.Sub(Float.PI) : Az.Add(Float.PI); // so 0 degrees is north
        // h = atan2(zhor, sqrt(xhor * xhor + yhor * yhor));
        h = Float.atan2(zhor, Float.sqrt(xhor.Mul(xhor).Add(yhor.Mul(yhor))));
        return new HorizontalPosition(Float.toDegrees(Az), Float.toDegrees(h));
    }
}
